package goldeneagle.com.admin.servlet;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.alibaba.fastjson.JSONObject;

import goldeneagle.com.admin.dao.impl.AdminManagerDaoImpl;
import goldeneagle.com.entity.User;

/**
 * Servlet implementation class AdminUpdateUserServlet
 */
@WebServlet(value="/AdminUpdateUserServlet")
public class AdminUpdateUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminUpdateUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> iter = items.iterator();
			Map<String, String> map = new HashMap<String, String>();
			while (iter.hasNext()) {
				FileItem item = iter.next();
				String itemName = item.getFieldName();
				if (item.isFormField()) {
					map.put(itemName, item.getString("UTF-8"));
				} else {
					String fileName = item.getName();
					if (fileName != null && !fileName.equals("")) {
						String path = request.getSession().getServletContext().getRealPath("upload");
						File file = new File(path, fileName);
						item.write(file);
					}
					map.put(itemName, fileName);
				}
			}
			AdminManagerDaoImpl ad = new AdminManagerDaoImpl();
			User user = null;
			if (map.get("img") == null || map.get("img").equals("")) {
				user = new User(Integer.parseInt(map.get("id")),map.get("username"),map.get("password"),map.get("address"),map.get("email"),map.get("imgName"),map.get("userType"));
			} else {
				user = new User(Integer.parseInt(map.get("id")),map.get("username"),map.get("password"),map.get("address"),map.get("email"),map.get("img"),map.get("userType"));
			}
			if(ad.adminUpdateUser(user))  {
				request.getRequestDispatcher("AdminPageSelectUserServlet").forward(request, response);
			}else {
				response.getWriter().write("<script> alert('更新失败!') ;window.location='AdminPageSelectUserServlet'</script>");
				//request.getRequestDispatcher("AdminPageSelectUserServlet").forward(request, response);
			}

		} catch (FileUploadException e) {

			e.printStackTrace();
		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
